package com.jy.appmarket.activity;

import com.jy.appmarket.AppMarketApplication;
import com.jy.appmarket.R;
import com.jy.appmarket.dialog.WaitDialog;
import com.jy.appmarket.network.engine.BaseNetEngine;
import com.jy.appmarket.network.engine.ModifyPasswdNetEngine;
import com.jy.appmarket.network.resultdata.ModifyPasswdResult;
import com.jy.appmarket.network.task.NetTask;
import com.jy.appmarket.network.task.NetTask.NetTaskListener;
import com.jy.appmarket.utils.CryptoUtil;
import com.jy.appmarket.utils.StringUtil;
import com.umeng.analytics.MobclickAgent;

import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;

public class ModifyPasswdActivity extends Activity implements OnClickListener, NetTaskListener{
	private EditText mOldPwdEdit;
	private EditText mNewPwdEdit;
	private WaitDialog mWaitDialog;
	private NetTask mNetTask;
	private String mPhoneNum;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_modify_passwd);
		
		initTitle();
		initViews();
	}

	private void initTitle() {
		ActionBar actionBar = getActionBar();
		actionBar.setTitle("修改密码");
		actionBar.setDisplayUseLogoEnabled(false);
		actionBar.setDisplayShowHomeEnabled(false);
		actionBar.setDisplayHomeAsUpEnabled(true);
	}
	
	private void initViews() {
		mOldPwdEdit = findViewById(R.id.old_pwd_edit);
		mNewPwdEdit = findViewById(R.id.new_pwd_edit);
		findViewById(R.id.modify_pwd_btn).setOnClickListener(this);
		
		if (AppMarketApplication.isLogin()) {
			mPhoneNum = AppMarketApplication.getLogin().getPhoneNum();
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.modify_pwd_btn:
			modifyPasswd();
			break;

		default:
			break;
		}
	}
	
	private void modifyPasswd() {
		String oldPasswd = mOldPwdEdit.getText().toString();
		String newPasswd = mNewPwdEdit.getText().toString();

		if (TextUtils.isEmpty(oldPasswd)) {
			Toast.makeText(this, R.string.please_input_old_password,
					Toast.LENGTH_SHORT).show();
			return;
		}
		if (TextUtils.isEmpty(newPasswd)) {
			Toast.makeText(this, R.string.please_input_new_password,
					Toast.LENGTH_SHORT).show();
			return;
		}
		if (StringUtil.hasSpaceChar(newPasswd)) {
			Toast.makeText(this, R.string.password_not_has_space,
					Toast.LENGTH_SHORT).show();
			return;
		}
		if (oldPasswd.length() < 6 || newPasswd.length() < 6) {
			Toast.makeText(this, R.string.password_format_error,
					Toast.LENGTH_SHORT).show();
			return;
		}
		
		showWaitDialog();
		ModifyPasswdNetEngine engine = new ModifyPasswdNetEngine(mPhoneNum, CryptoUtil.md5(oldPasswd), CryptoUtil.md5(newPasswd));
		mNetTask = new NetTask(this, engine, 0);
		mNetTask.setListener(this);
		new Thread(mNetTask).start();
	}
	
	@Override
	public void onTaskRunSuccessful(int tag, BaseNetEngine engine) {
		dismissWaitDialog();
		ModifyPasswdResult resultData = (ModifyPasswdResult) engine.getResultData();
		if (!resultData.isOK()) {
			String message = resultData.getMessage();
			if (!TextUtils.isEmpty(message)) {
				Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
			} else {
				Toast.makeText(this, R.string.modify_password_error, Toast.LENGTH_SHORT).show();
			}
		} else {
			Toast.makeText(this, R.string.modify_password_success, Toast.LENGTH_SHORT).show();
			finish();
		}
	}

	@Override
	public void onTaskRunError(int tag) {
		dismissWaitDialog();
		Toast.makeText(this, R.string.network_error, Toast.LENGTH_SHORT).show();
	}

	@Override
	public void onTaskRunCanceled(int tag) {
		dismissWaitDialog();
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			finish();
			break;
		}
		return super.onOptionsItemSelected(item);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}

	@Override
	protected void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}
	
	@Override
	protected void onDestroy() {
		if (mNetTask != null) {
			mNetTask.cancel();
			mNetTask.setListener(null);
			mNetTask = null;
		}
		dismissWaitDialog();
		super.onDestroy();
	}
	
	private void showWaitDialog(){
		if (mWaitDialog == null) {
			mWaitDialog = new WaitDialog(this);
			mWaitDialog.setCanceledOnTouchOutside(false);
			mWaitDialog.setCancelable(false);
		}
		if (mWaitDialog.isShowing()) {
			return;
		}
		mWaitDialog.showTitle("请稍候...").show();
	}
	
	private void dismissWaitDialog(){
		if (mWaitDialog != null) {
			mWaitDialog.dismiss();
		}
	}

}
